<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-16
 * Time: 下午5:04
 */

namespace mybackend\controllers\ext;

use yii\web\Controller;
use yii\web\Response;
use common\services\common\MyscrmOrganizationService;
use common\services\outstock\OutStockOrderService;
use Yii;
use common\services\common\MyscrmContractProjectService;

class OutStockController extends Controller
{
    private $service;
    public function __construct($id, $module, OutStockOrderService $outStockOrderService, $config = [])
    {
        $this->service = $outStockOrderService;
        parent::__construct($id, $module, $config);
    }
    /*
    * Author yuyj
    * Date 2017-01-03
    * @apiVersion 1.0.0
    * @api {get} /api/index.php/out-stock-order/get-project-list 根据应用ID获取需要授权的项目列表
    * @apiGroup out-stock-order-controller
    *
    * @apiParam query {string} orgCode 租户code
    * @apiParam query {string} authorizeApplicationId 授权的应用ID
    *
    * @apiResponse 200 {
    * "data": [
    *       {
    *       "ProjId{项目ID}": "39dc524c-8563-3db6-8714-05cc316ca138",
    *       "ProjName{项目名称}": "yb123"
    *       }
    * ],
    * "retCode": "0",
    * "errMsg": ""
    * }
    */
    public function actionGetProjectsList()
    {
        $keyword=\Yii::$app->request->request("keyword","");
        $orgCode=\Yii::$app->request->request("org_code","");
        $applicationInfoId=\Yii::$app->request->request("application_id","");
        $status=\Yii::$app->request->request("status","");
        $status = $status?explode(',', $status):[];
        $isOverdue = 0;
        foreach ($status as $key => $item){
            if ($item == 0){
                $isOverdue = 1;
                unset($status[$key]);
                break;
            }
        }
        /** @var MyscrmContractProjectService $myscrmContractProjectService */
        $myscrmContractProjectService=\Yii::createObject(MyscrmContractProjectService::className());
        $result = $myscrmContractProjectService->getAuthorizeProjectList($orgCode, $applicationInfoId, $status, $isOverdue ,$keyword);
        $data=[];
        Yii::$app->response->format = Response::FORMAT_JSON;
        if(!empty($result)) {
            /** @var MyscrmOrganizationService $organizationService */
            $organizationService = \Yii::createObject(MyscrmOrganizationService::className());
            $org = $organizationService->getOrgByUniqueName($orgCode);
            if(!$org){
                $exportData['data'] = [];
                $exportData['code'] = __LINE__;
                $exportData['msg'] = '无法获取租户信息！';
                Yii::$app->response->data = $exportData;
                \Yii::$app->end();
            }
            foreach ($result as $key => $value) {
                $tmp = [];
                $tmp['org_code'] = $org->UniqueName;
                $tmp['org_name'] = $org->FriendlyName;
                $tmp['org_guid'] = $org->Id;
                $tmp['project_guid'] = $value['ProjId'];
                $tmp['project_name'] = $value['ProjName'];
                $tmp['contract_begin'] = $value['contractBeginOn'];
                $tmp['contract_end'] = $value['contractEndOn'];
                $tmp['status'] = $value['status'];
                $data[] = $tmp;
            }
        }
        $exportData['data'] = $data;
        $exportData['code'] = $this->service->getErrCode();
        $exportData['msg'] = $this->service->getErrMsg();
        Yii::$app->response->data = $exportData;
        \Yii::$app->end();
    }

    //通过关键字获取租户列表
    public function actionGetOrgsList(){
        $keyword=\Yii::$app->request->request("keyword","");
        /** @var MyscrmOrganizationService $organizationService */
        $organizationService=\Yii::createObject(MyscrmOrganizationService::className());
        $result = $organizationService->getOrgsListByKeyword($keyword,1,10,"Id,UniqueName,FriendlyName",false);
        $data=[];
        if(!empty($result)) {
            foreach ($result['list'] as $org) {
                $tmp['platform_code'] = 'yunke';
                $tmp['platform_name'] = '云客';
                $tmp['company_code'] = $org->UniqueName;
                $tmp['company_name'] = $org->FriendlyName;
                $data[] = $tmp;
            }
        }
        Yii::$app->response->format = Response::FORMAT_JSON;
        $exportData['data'] = $data;
        Yii::$app->response->data = $exportData;
        \Yii::$app->end();
    }
}